#!/usr/bin/env python

import os, datetime

"""
计算某一天与今天之间差了几天
"""

NOT_SET = "未设置日期"

try:
    config_file_path = os.path.join(os.environ['HOME'], '.date.txt')
    content = open(config_file_path).readlines()
except FileNotFoundError as e:
    print(NOT_SET)
    exit(0)

# 日期存放在 $HOME/.date.txt 文件夹下
# 该文件共两行:
#  yyyy-mm-dd
#  提示文本
# 程序输出: 今日是{提示文本}的第XXX天
# 不检查格式正确性, 自行确保格式正确

year, month, day = tuple(content[0].strip().split('-'))
year, month, day = int(year), int(month), int(day)
placeholder = content[1].strip()

# 获取今日时间
now:datetime.datetime = datetime.datetime.now()
mark:datetime.datetime = datetime.datetime(year=year, month=month, day=day)

def get_dhms(t1:datetime.datetime, t2:datetime.datetime)->tuple[int, int, int, int]:
    interval:list[str] = f"{t1 - t2}".split()
    if len(interval) > 1:
        d:int = int(interval[0])
    else:
        d = 0
    h, m, s = interval[2].split('.')[0].split(':')
    h, m, s = int(h), int(m), int(s)
    return (d, h, m, s)

d, h, m, s = get_dhms(t1=now, t2=mark)

to_mark = False
if d < 0 or h < 0 or m < 0 or s < 0:
    to_mark = True
    d, h, m, s = get_dhms(t1=mark, t2=now)

if to_mark:
    if d > 0:
        print(f"距离{placeholder}还剩{d}天{h}小时{m}分")
    else:
        print(f"距离{placeholder}还剩{h}小时{m}分")
else:
    if d > 0:
        print(f"已经{placeholder}了{d}天{h}小时{m}分")
    else:
        print(f"已经{placeholder}了{h}小时{m}分")
